package _01_第一季.树04.Tree02;

import org.junit.Test;
import _01_第一季.树04.printer.BinaryTrees;

public class Main {
    @Test
    public void testPerOrder(){
        Integer[] nums = {6,4,7,2,5,1,3};
        BST<Integer> bst = new BST<>();
        for (Integer num : nums) {
            bst.add(num);
        }
        BinaryTrees.println(bst);
        bst.preOrder(new BinaryTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return false;
            }
        });
    }

    @Test
    public void testInOrder(){
        Integer[] nums = {6,4,7,2,5,1,3};
        BST<Integer> bst = new BST<>();

        for (Integer num : nums) {
            bst.add(num);
        }
        BinaryTrees.println(bst);
        bst.inOrder(new BinaryTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return false;
            }
        });
    }

    @Test
    public void testPostOrder(){
        Integer[] nums = {6,4,7,2,5,1,3};
        BST<Integer> bst = new BST<>();

        for (Integer num : nums) {
            bst.add(num);
        }
        BinaryTrees.println(bst);
        bst.postOrder(new BinaryTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element+" ");
                return false;
            }
        });
    }

    @Test
    public void testAvl(){
        Integer[] datas = {10, 100, 42, 59, 97, 91};
        AVLTree<Integer> avl = new AVLTree<>();
        for (int i = 0; i < datas.length; i++) {
            avl.add(datas[i]);
        }
        BinaryTrees.println(avl);

        for (int i = 0; i < datas.length; i++) {
            avl.remove(datas[i]);
            System.out.println("["+datas[i]+"]");
            BinaryTrees.println(avl);
            System.out.println("---------------------");
        }
    }

    @Test
    public void testRBTree(){
        Integer[] datas = {55,87,56,74,96,22,62,20,70,68,90,50};
        RBTree<Integer> rbTree = new RBTree<>();
        for (int i = 0; i < datas.length; i++) {
            rbTree.add(datas[i]);
        }
        BinaryTrees.println(rbTree);
        for (int i = 0; i < datas.length; i++) {
            rbTree.remove(datas[i]);
            System.out.println("【"+datas[i]+"】");
            BinaryTrees.println(rbTree);
            System.out.println("------------------------");
        }
    }
}
